//
// Created by xusun on 2022/1/27.
//

#include<iostream>
#include<vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

using namespace std;

class Solution124 {
private:
    int maxSum = INT_MIN;

    int getMaxPath(TreeNode *root) {
        if (root == NULL)return 0;
        int left = max(getMaxPath(root->left), 0);
        int right = max(getMaxPath(root->right), 0);
        maxSum = max(maxSum, root->val + left + right);
        return max(left, right) + root->val;
    }

public:
    int maxPathSum(TreeNode *root) {
        getMaxPath(root);
        return maxSum;
    }
};